package com.goldenautumn.prefabricate.activity.mine.order

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.LogUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.cart.SettlementGoodsAdapter
import com.goldenautumn.prefabricate.activity.mine.adapter.ImgSelectAdapter
import com.goldenautumn.prefabricate.activity.mine.adapter.LogisticsAdapter
import com.goldenautumn.prefabricate.business.entity.ExpressInfo
import com.goldenautumn.prefabricate.business.entity.ExpressItem
import com.goldenautumn.prefabricate.business.mall.GoodsInfoUiState
import com.goldenautumn.prefabricate.business.order.OrderStatusUiState
import com.goldenautumn.prefabricate.business.order.OrderViewModel
import com.goldenautumn.prefabricate.business.user.LoadingUiState
import com.goldenautumn.prefabricate.databinding.ActivityLogisticsBinding
import com.goldenautumn.prefabricate.utils.Constants
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA
import com.goldenautumn.prefabricate.utils.onCenterToast
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseActivity
import qncx.external.common.compat.gone

class LogisticsActivity : BaseActivity<ActivityLogisticsBinding>() {

    private val mOrderModel by viewModels<OrderViewModel>()

    private val mAdapter by lazy {
        LogisticsAdapter()
    }

    private var orderSn:String? = null
        get() = intent.getStringExtra(BUSINESS_PAGE_DATA)

    override fun getLayoutId(): Int = R.layout.activity_logistics

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        initToolbarNav(mBinding.toolbar)

        mOrderModel.onExpressInfo("$orderSn")

        mBinding.logisticsRv.layoutManager = LinearLayoutManager(this)
        mBinding.logisticsRv.adapter = mAdapter

        mBinding.logisticsSn.setOnClickListener {
            ClipboardUtils.copyText(mBinding.logisticsSn.text.toString())
            onCenterToast(getString(qncx.external.common.R.string.common_copy))
        }


        while (isActive) {
            select {
                mOrderModel.loading.onReceive {
                    when (it) {
                        is LoadingUiState.LoadingUI -> {
                            if (!it.isShow) LogUtils.d("加载结束")
                        }

                        else -> {}
                    }
                }
                mOrderModel.orderStatusUiState.onReceive {
                    when (it) {
                        is OrderStatusUiState.ExpressInfo -> {
                            mBinding.type.text = "${it.express.express_name}"
                            mBinding.logisticsSn.text = "${it.express.express_no}"
                            var data = ArrayList<ExpressItem>()
                            var pickUp = it.express.list?.filter { it.status == getString(R.string.logistics_state_1) }
                            var way = it.express.list?.filter { it.status == getString(R.string.logistics_state_2) }
                            var delivery = it.express.list?.filter { it.status == getString(R.string.logistics_state_3) }
                            var sign = it.express.list?.filter { it.status == getString(R.string.logistics_state_4) }

                            if(sign?.size != 0) {
                                data.add(ExpressItem(
                                    icon = getExpressItem("${sign?.get(0)?.status}",true),
                                    statusName = "${sign?.get(0)?.status}",
                                    items = sign
                                ))
                            }
                            if(delivery?.size != 0) {
                                data.add(ExpressItem(
                                    icon = getExpressItem("${delivery?.get(0)?.status}",sign?.size == 0),
                                    statusName = "${delivery?.get(0)?.status}",
                                    items = delivery
                                ))
                            }
                            if(way?.size != 0) {
                                data.add(ExpressItem(
                                    icon = getExpressItem("${way?.get(0)?.status}",delivery?.size == 0),
                                    statusName = "${way?.get(0)?.status}",
                                    items = way
                                ))
                            }
                            if(pickUp?.size != 0) {
                                data.add(ExpressItem(
                                    icon = getExpressItem("${pickUp?.get(0)?.status}",way?.size == 0),
                                    statusName = "${pickUp?.get(0)?.status}",
                                    items = pickUp
                                ))
                            }
                            mAdapter.submitList(data)
                        }

                        else -> {}
                    }
                }
            }
        }
    }

    fun getExpressItem(type:String,isExit:Boolean):Int{
        return if(type == "揽收") {
            if(isExit) R.mipmap.ic_logistics_1 else R.mipmap.ic_logistics_1_not
        } else if(type == "在途") {
            if(isExit) R.mipmap.ic_logistics_2 else R.mipmap.ic_logistics_2_not
        } else if(type == "派件") {
            if(isExit) R.mipmap.ic_logistics_3 else R.mipmap.ic_logistics_3_not
        } else {
            if(isExit) R.mipmap.ic_logistics_4 else R.mipmap.ic_logistics_4_not
        }
    }


}